package com.amtf.datafile3.cfg.excel;

import java.text.DateFormat;
import java.text.Format;
import java.text.NumberFormat;

import org.apache.poi.ss.usermodel.FormulaEvaluator;

import com.amtf.datafile3.DataField;
import com.amtf.datafile3.DataFieldRender;
import com.amtf.datafile3.DataFieldType;
import com.amtf.datafile3.cfg.std.StdDataFieldDefinition;
import com.amtf.datafile3.core.excel.parser.AbsExcelDataFieldParser;
import com.amtf.datafile3.core.excel.parser.ExcelDateFieldParser;
import com.amtf.datafile3.core.excel.parser.ExcelDecimalFieldParser;
import com.amtf.datafile3.core.excel.parser.ExcelStringFieldParser;
import com.amtf.datafile3.core.excel.render.ExcelDataFieldRender;
import com.amtf.datafile3.core.excel.render.ExcelDecimalFieldRender;
import com.amtf.datafile3.core.std.type.DateFieldType;
import com.amtf.datafile3.core.std.type.DecimalFieldType;
import com.amtf.datafile3.core.std.type.StringFieldType;

public class ExcelDataFieldDefinition extends StdDataFieldDefinition {

	public ExcelDataFieldDefinition(String name, DataFieldType type, boolean trimValue, Format informat, Format outformat, FormulaEvaluator evaluator) {
		super(name, type, createParser(type, evaluator, informat, trimValue), createRender(type, outformat));
	}

	private static DataFieldRender createRender(DataFieldType type, Format outformat) {
		if (type instanceof DecimalFieldType) {
			return new ExcelDecimalFieldRender((NumberFormat) outformat, '\"', ',', false);
		} else {
			return new ExcelDataFieldRender(outformat, '\"', ',', false);
		}
	}

	@Override
	public ExcelDataFieldDefinition clone() {
		ExcelDataFieldDefinition newObj = new ExcelDataFieldDefinition(name, null, false, null, null, null);
		copyTo(newObj);
		return newObj;
	}

	private static AbsExcelDataFieldParser<?, ?> createParser(DataFieldType type, FormulaEvaluator evaluator, Format informat, boolean trimValue) {
		AbsExcelDataFieldParser<? extends DataField, ?> fieldParser = null;
		if (type instanceof StringFieldType) {
			fieldParser = new ExcelStringFieldParser(evaluator, informat, trimValue);
		} else if (type instanceof DecimalFieldType) {
			fieldParser = new ExcelDecimalFieldParser(evaluator, (NumberFormat) informat, trimValue);
		} else if (type instanceof DateFieldType) {
			fieldParser = new ExcelDateFieldParser(evaluator, (DateFormat) informat, trimValue);
			// } else if (type instanceof ObjectFieldType) {
			// fieldParser = new ExcelStringFieldParser(quote, trimValue);
		} else {
			fieldParser = new ExcelStringFieldParser(evaluator, informat, trimValue);
		}

		return fieldParser;
	}

	public boolean isTrimValue() {
		return ((AbsExcelDataFieldParser) parser).isTrimValue();
	}

	public void setTrimValue(boolean trimValue) {
		((AbsExcelDataFieldParser) parser).setTrimValue(trimValue);
	}

	public Format getInformat() {
		return ((AbsExcelDataFieldParser) parser).getFormat();
	}

	public void setInformat(Format informat) {
		((AbsExcelDataFieldParser) parser).setFormat(informat);
	}

	public Format getOutformat() {
		return ((AbsExcelDataFieldParser) render).getFormat();
	}

	public void setOutformat(Format outformat) {
		((AbsExcelDataFieldParser) render).setFormat(outformat);
	}

	public FormulaEvaluator getEvaluator() {
		return ((AbsExcelDataFieldParser) parser).getEvaluator();
	}

	public void setEvaluator(FormulaEvaluator evaluator) {
		((AbsExcelDataFieldParser) parser).setEvaluator(evaluator);
	}

}
